package com.wannengbang.cloudleader.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wannengbang.cloudleader.appconst.Constants;
import com.wannengbang.cloudleader.event.PaySuccessEvent;
import com.wannengbang.cloudleader.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Constants.WENXIN_KEY, false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            //支付成功
            if (resp.errCode == 0) {
                ToastUtil.showShort("支付成功");
                EventBus.getDefault().post(new PaySuccessEvent(0));
                finish();
            } else if (resp.errCode == -1) {
                ToastUtil.showShort("支付失败");
                EventBus.getDefault().post(new PaySuccessEvent(-1));
                finish();
            } else if (resp.errCode == -2) {
                ToastUtil.showShort("支付取消");
                EventBus.getDefault().post(new PaySuccessEvent(-2));
                finish();
            }
        }
    }

}
